package com.ad.admin.filemanage;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private ListView list;
    private TextView nowPath;
    private MyAdapter myAdapter;
    private List<Map<String,Object>> values;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Globals.init(this);

        list = (ListView)findViewById(R.id.list);
        nowPath = (TextView)findViewById(R.id.nowPath);
        values = new ArrayList<>();
        File fs = Environment.getExternalStorageDirectory();
        loadFile(fs);
        myAdapter = new MyAdapter(values,this);
        list.setAdapter(myAdapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Map<String,Object> map = values.get(position);
                boolean isDir = (Boolean)map.get("isDir");
                File file = new File(map.get("fullPath").toString());
                Log.i("gtq00",file.getAbsolutePath()+"...........");
                String regex = "^\\.*";
                if(file.getName().matches(regex)){
                    return;
                }
                if(isDir){
                    values.clear();
                    if(!Environment.getExternalStorageDirectory().getAbsolutePath().equals(file.getAbsolutePath())){
                        Map<String,Object> parentData = new HashMap<String, Object>();
                        parentData.put("extName","open_dir");
                        parentData.put("fileName","返回上一级");
                        parentData.put("isDir", true);
                        parentData.put("fullPath", file.getParent());
                        parentData.put("real", "TRUE");
                        values.add(parentData);
                    }
                    loadFile(file);
                    //告诉adpater数据改变了
                    myAdapter.notifyDataSetChanged();
                }else{
                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                    builder.setTitle("文件信息 ");
                    builder.setMessage("文件大小为:" + file.length() + "bite");

                    builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });
                    builder.create().show();
                }

            }
        });
        list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
                final Map<String,Object> map = values.get(position);
                boolean isDir = (Boolean)map.get("isDir");
                if(!isDir){
                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                    builder.setTitle("提示信息");
                    builder.setMessage("你确定 要删除吗?");
                    builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            File file = new File(map.get("fullPath").toString());
                            file.delete();
                            values.remove(position);
                            myAdapter.notifyDataSetChanged();
                        }
                    });
                    builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });
                }
                return false;
            }
        });

    }

    private void loadFile(File fs) {

        File[] files = fs.listFiles();
        nowPath.setText(fs.getAbsolutePath());
        for(File f : files){
            Map<String,Object> map = new HashMap<>();
            map.put("fileName", f.getName());
            map.put("fullPath", f.getAbsolutePath());
            if(f.isDirectory()){
                map.put("isDir",true);
                map.put("extName","close_dir");
            }else{
                map.put("isDir",false);
                map.put("extName",f.getName().substring(f.getName().lastIndexOf(".")+1).toLowerCase());
            }
            values.add(map);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            Map<String,Object> map = values.get(0);
            if("TRUE".equals(map.get("real"))){
                list.performItemClick(list.getChildAt(0),0,list.getChildAt(0).getId());
            }else{
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("提示信息");
                builder.setMessage("确定要退出吗");
                builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                });
                builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.create().show();

            }
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }
}
